<?php

/**
 * @Author: xuebingsi
 * @Date:   2018-08-24 08:07:06
 * @Last Modified by:   zhibinm
 * @Last Modified time: 2018-08-26 11:22:46
 */
/**
 * 数据库操作类
 */
class Db
{
	public static $pdo;
	public static $instance;
	public $errorInfo;
	public $table;
	public $where ='';
	public $order = "";
	public $limit = "";
	// 构造函数
	public function __construct($table=null)
	{
		if($table){
			$this->table = $table;
		}
		try {
			// 链接数据库
			self::$pdo = new PDO(config('db_type').":host=".config('db_host').";dbname=".config('db_name').";charset=".config('db_charset'),config('db_user'),config('db_pass'));

		} catch (PDOException $e) {
			$this->errorInfo= "数据执行失败";
		}
	}
	//执行原生sql
	public static function exec($sql)
	{
		if(!self::$instance){
			self::$instance = new self();
		}

		return self::$pdo->exec($sql);

	}
	//执行原生sql
	public static function query($sql)
	{
		if(!self::$instance){
			self::$instance = new self();
		}

		$res = self::$pdo->query($sql);

		if($res){
			return $res->fetchAll(PDO::FETCH_ASSOC);
		}else{
			self::$instance->errorInfo = self::$pdo->errorInfo();
			return false;
		}
	}
	//获取错误信息
	public static function getError()
	{
		return self::$instance->errorInfo;
	}
	//增加
	public function save($data)
	{

		$key = implode(',',array_keys($data));

		$values = implode("','",$data);

		// $sql = "insert into stu (name,age) values('小明',20)";
		$sql = "insert into ".$this->table." (".$key.") values('".$values."')";

		$res = self::$pdo->exec($sql);

		if($res){
			return $res;
		}else{
			$this->errorInfo = self::$pdo->errorInfo();
		}
	}
	//删除
	public function del($id)
	{
		if(is_array($id)){

			$key = current(array_keys($id));

			$value = current($id);

			$sql = "delete from ".$this->table." where ".$key."='".$value."'";
		}else{

			$preKey = $this->getPreKey();
			// $sql = "delete from stu where sid=9";
			$sql = "delete from ".$this->table." where ".$preKey."=".$id;
		}

		return self::$pdo->exec($sql);
	}
	// 获取主键
	public function getPreKey()
	{
		$sql = "desc ".$this->table;

		$data = self::$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);

		foreach ($data as $row) {
			if($row['Key']=='PRI'){
				return $row['Field'];
			}
		}
	}
	// where
	public function where($where)
	{
		if(is_array($where)){
			$this->where = $where;
		}else{
			$this->where = " where ".$where;
		}
	}
	// 更新
	public function update($data)
	{

		$key = current(array_keys($this->where));

		$value = current($this->where);

		$str = '';

		foreach ($data as $k => $v) {
			$str .= $k."='".$v."',";
		}

		$str  = rtrim($str,',');

		// $sql = "update stu set name='小明',age='20' where sid=9";

		$sql = "update ".$this->table." set ".$str." where ".$key."='".$value."'";

		return self::$pdo->exec($sql);

	}
	// 取几条
	public function limit($limit)
	{
		$this->limit = " limit ".$limit;
	}
	// 排序
	public function order($order)
	{
		$this->order = " order by ".$order;
	}
	// 查询 
	public function select()
	{
		// $sql = "select * from stu where sex='女' order by age desc limit 5";

		$sql = "select * from ".$this->table.$this->where.$this->order.$this->limit;

		$res = self::$pdo->query($sql);

		if($res){
			return $res->fetchAll(PDO::FETCH_ASSOC);
		}else{
			self::$instance->errorInfo = self::$pdo->errorInfo();
			return false;
		}
	}
}